# Create your models here.
from django.db import models
from rest_framework import serializers


class Article(models.Model):
    title = models.CharField('标题', max_length=100)

    content = models.TextField('内容')

    type_id = models.CharField(
        '类型',
        max_length=20,
    )

    author = models.CharField(
        '作者',
        max_length=20,
    )

    update_time = models.DateTimeField('更新时间', auto_now=True)

    is_active = models.BooleanField(
        '是否启用',
        default=True
    )

    class Meta:
        db_table = 'sys_articles'
        verbose_name = '文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class SysArticleSerializer(serializers.ModelSerializer):
    type_name = serializers.SerializerMethodField()

    class Meta:
        model = Article
        fields = "__all__"

    def get_type_name(self, obj):
        # 直接访问注解字段
        return obj.type_name
